package com.shaw.study.springx.transaction;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * Created by asus on 2018/5/15.
 */
public class ThreadConnectionHolder {
    public static ThreadLocal<ConnectionHolder> threadLocal = new ThreadLocal<>();
    private static ConnectionHolder getConnectionHolder(){
        ConnectionHolder connectionHolder = threadLocal.get();
        if(connectionHolder==null){
            connectionHolder = new ConnectionHolder();
            threadLocal.set(connectionHolder);
        }
        return connectionHolder;
    }

    public static Connection getConnection(DataSource dataSource) throws SQLException {
        return getConnectionHolder().getConnectionByDataSource(dataSource);
    }
}
